/*
 * @Author: szx
 * @Date: 2022-03-30 13:22:06
 * @LastEditTime: 2022-03-30 13:49:47
 * @Description:
 * @FilePath: \leetcode\剑指offerII\001\001.js
 */
/**
 * @param {number} a
 * @param {number} b
 * @return {number}
 */
var divide = function (a, b) {
    const INT_MIN = -Math.pow(2, 31);
    const INT_MAX = Math.pow(2, 31) - 1;

    if (a == INT_MIN && b == -1) return INT_MAX;

    let res = 0;
    let flag = true;
    if (a < 0) {
        a = -a;
        flag = !flag;
    }
    if (b < 0) {
        b = -b;
        flag = !flag;
    }
    if (a < b) return 0;
    while (a >= b) {
        let value = b;
        let k = 1;
        while (value > 0 && a >= value + value) {
            value += value;
            k += k;
        }
        a -= value;
        res += k;
    }
    return flag ? res : -res;
};
console.log(divide(15, 2));
console.log(divide(7, -3));
console.log(divide(0, 1));
console.log(divide(-2, 1));
console.log(divide(-1, -1));
